// (C) Copyright 1990-2002 by Autodesk, Inc. 
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted, 
// provided that the above copyright notice appears in all copies and 
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting 
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. 
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC. 
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to 
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

//-----------------------------------------------------------------------------
//----- CEdSSgetFilter.cpp : Implementation of CEdSSgetFilter
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "CEdSSgetFilter.h"

//-----------------------------------------------------------------------------
CEdSSgetFilter::CEdSSgetFilter (AcApDocument *pDoc) : AcEdSSGetFilter () {
	mpDoc = NULL ;
	if (pDoc != NULL) {
		mpDoc = pDoc ;
		addSSgetFilterInputContextReactor(mpDoc, this);
	}
}

//-----------------------------------------------------------------------------
CEdSSgetFilter::~CEdSSgetFilter () {
	Detach () ;
}

//-----------------------------------------------------------------------------
void CEdSSgetFilter::Attach () {
	Detach () ;
	mpDoc = acDocManager->curDocument () ;
	if (mpDoc) addSSgetFilterInputContextReactor(mpDoc, this);
}

void CEdSSgetFilter::Detach () {
	if (mpDoc) {
		removeSSgetFilterInputContextReactor(mpDoc, this);
		mpDoc =NULL ;
	}
}

// -----------------------------------------------------------------------------
void CEdSSgetFilter::endSSGet(Acad::PromptStatus returnStatus, int ssgetFlags, AcEdSelectionSetService & service, const AcDbObjectIdArray & selectionSet)
{
	AcEdSSGetFilter::endSSGet (returnStatus, ssgetFlags, service, selectionSet) ;
	ACHAR *globalCmdName = NULL;
	if (acedGetCommandForDocument(mpDoc, globalCmdName) == Acad::eOk) {
		////////////////////////////////////////////////////////////////////////////////////////////////////////
		// To disable pickfirst selection for some classes, then you have to check globalCmdName is empty string
		// if (isCheckedCommand(globalCmdName) || AcString(globalCmdName).isEmpty())
		////////////////////////////////////////////////////////////////////////////////////////////////////////
		if (isCheckedCommand(globalCmdName)) {
			for (int i = 0; i < selectionSet.length(); i++)	{
				AcDbEntityPointer pEnt(selectionSet[i],AcDb::kForRead);
				if (pEnt.openStatus() == Acad::eOk)	{
					if (isCheckedClass(pEnt->isA())) {
						service.remove(i);
					}
				}
			}
		}
		acutDelString(globalCmdName);
	}
}

void CEdSSgetFilter::endEntsel(Acad::PromptStatus  returnStatus, const AcDbObjectId&  pickedEntity,	const AcGePoint3d& pickedPoint,	AcEdSelectionSetService& service)
{
	ACHAR *globalCmdName = NULL;
	if (acedGetCommandForDocument(mpDoc, globalCmdName) == Acad::eOk) {
		if (isCheckedCommand(globalCmdName)) {
			AcDbEntityPointer pEnt(pickedEntity,AcDb::kForRead);
			if (pEnt.openStatus() == Acad::eOk)	{
				if (isCheckedClass(pEnt->isA())) {
					service.remove(0);
				}
			}
		}
		acutDelString(globalCmdName);
	}
}